package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.dto.ShopDetailSalesDto;
import com.yami.shop.bean.model.CheckAccount;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopType;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.CheckAccountMapper;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.dao.ShopDetailMapper;
import com.yami.shop.service.CheckAccountService;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CheckAccountServiceImpl extends ServiceImpl<CheckAccountMapper, CheckAccount> implements CheckAccountService {
    @Autowired
    private CheckAccountMapper checkAccountMapper;

    @Autowired
    private  CheckAccountService checkAccountService;

    @Autowired
    private ShopTypeService shopTypeService;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private OrderService orderService;

    @Override
    public Order getOrderDetailBynumber(String orderNumber) {
        return null;
    }
    @Override
    public CheckAccount getByOrderNumber(String orderNumber) {
        return checkAccountMapper.getByOrderNumber(orderNumber);
    }

    @Override
    public IPage<CheckAccount> pageCheckAccountByOrderParam(Page<CheckAccount> page, CheckAccount checkAccount) {
         page.setRecords(checkAccountMapper.listcheckAccountByOrderParam(new PageAdapter(page),checkAccount));
         page.setTotal(checkAccountMapper.countAccount(checkAccount));
         return page;
    }

    @Override
    public Double getRebateMoneyAmount() {
        return checkAccountMapper.getRebateMoneyAmount();
    }

    @Override
    public Double getShopBusinessVolume(Long shopId) {
        return checkAccountMapper.getShopBusinessVolume(shopId);
    }

    @Override
    public Double getShopBusinessVolumeByTime(ShopDetailSalesDto shopDetailSalesDto, Long shopId) {
        return checkAccountMapper.getShopBusinessVolumeByTime(shopDetailSalesDto,shopId);
    }

    @Override
    @Transactional
    public void dealOrderByTime(Order order) {
        //生成入账记录表,增加商户可提现余额
        CheckAccount checkAccount1 = new CheckAccount();
        checkAccount1.setOrderNumber(order.getOrderNumber());
        checkAccount1.setType("入账");
        checkAccount1.setPayAmount(order.getActualTotal());
        checkAccount1.setTime(new Date());
        checkAccount1.setShopid(order.getShopId());
        checkAccount1.setDeliveryPrice(order.getDeliveryPrice());

        //更改订单状态,修改订单状态为6(已完结状态)
        order.setStatus(6);
        orderService.updateById(order);

        //计算服务费
        Long shopId = order.getShopId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        Double withdrawalAmount =0.0;
        Double serviceFee =0.0;
        if(shopDetail!=null){
            int shopTypeId = shopDetail.getShopType();
            ShopType shopType = shopTypeService.getById(shopTypeId);
            if(shopType!=null){
                //普通费率
                double rate = shopType.getRate();
                //推广费率
                double promoteRate = shopType.getPromoteFee();
                //计算服务费的金额
                serviceFee = Arith.mul(order.getActualTotal(),rate/100);
                //计算推广费的金额
                double promoteFee = Arith.mul(order.getActualTotal(),promoteRate/100);
                if(order.getMixedPayment()==1){
                    if(null!=order.getNewAccountPay()&&order.getNewAccountPay().compareTo(new BigDecimal("0"))>-1){
                        //计算账户支付的服务费金额
                        serviceFee = Arith.mul(order.getNewAccountPay().doubleValue(),rate/100);
                        //计算推广费的金额
                        Double remainPromoteFee = Arith.mul(order.getActualTotal().doubleValue(),promoteRate/100);
                        checkAccount1.setService_fee(serviceFee);
                        checkAccount1.setPromotePrice(remainPromoteFee);
                        withdrawalAmount = Arith.sub(Arith.sub(order.getActualTotal(),serviceFee),remainPromoteFee);
                        checkAccount1.setWithdrawalAmount(withdrawalAmount);
                    }else if(null!=order.getUnionPay()&&order.getUnionPay().compareTo(new BigDecimal("0"))>-1){
                        //计算账户支付的服务费金额
                        serviceFee = Arith.mul(order.getUnionPay().doubleValue(),rate/100);
                        checkAccount1.setService_fee(serviceFee);
                        withdrawalAmount = Arith.sub(order.getActualTotal(),serviceFee);
                        checkAccount1.setWithdrawalAmount(withdrawalAmount);
                    }else if(null!=order.getCashPay()&&order.getCashPay().compareTo(new BigDecimal("0"))>-1){
                        //计算账户支付的服务费金额
                        serviceFee = Arith.mul(order.getCashPay().doubleValue(),rate/100);
                        checkAccount1.setService_fee(serviceFee);
                        withdrawalAmount = Arith.sub(order.getActualTotal(),serviceFee);
                        checkAccount1.setWithdrawalAmount(withdrawalAmount);
                    }
                }else{
                    if(order.getPayType()==3) {
                        checkAccount1.setService_fee(serviceFee);
                        checkAccount1.setPromotePrice(promoteFee);
                        //可提现金额
                        withdrawalAmount = Arith.sub(Arith.sub(order.getActualTotal(),serviceFee),promoteFee);
                        checkAccount1.setWithdrawalAmount(withdrawalAmount);
                    }else{
                        checkAccount1.setService_fee(serviceFee);
                        withdrawalAmount = Arith.sub(order.getActualTotal(),serviceFee);
                        checkAccount1.setWithdrawalAmount(withdrawalAmount);
                    }
                }
            }
        }
        checkAccountService.save(checkAccount1);

        //商家增加可提现金额
        Double account = shopDetail.getAccount();
        Double rebateAccount = shopDetail.getRebateMoney();
        Double cashmoney = shopDetail.getCashMoney();
        Double delivery = shopDetail.getDeliveryPrice();
        if(account==null){
            account=0.00;
        }
        if(rebateAccount==null){
            rebateAccount = 0.00;
        }

        if(cashmoney==null){
            cashmoney = 0.00;
        }

        if(delivery==null){
            delivery = 0.00;
        }

        //把订单分服务费给平台账户加上
        ShopDetail platform = shopDetailService.getShopDetailByShopId(1L);
        Double platformCashAccount = platform.getCashMoney();
        if(platformCashAccount==null){
            platformCashAccount=0.00;
        }
        Double sumPlatformCashAccount = Arith.add(platformCashAccount,serviceFee);
        platform.setCashMoney(sumPlatformCashAccount);
        shopDetailService.updateById(platform);

        Double sumAccount = Arith.add(order.getActualTotal() , account);
        shopDetail.setAccount(sumAccount);
        Double sumCashMoney = Arith.add(cashmoney,withdrawalAmount);
        shopDetail.setCashMoney(sumCashMoney);
//        Double sumDeliveryPrice = Arith.add(delivery,order.getDeliveryPrice());
//        shopDetail.setDeliveryPrice(sumDeliveryPrice);
        shopDetailService.updateById(shopDetail);


    }
}
